package com.example.casper.Experiment2024.data;

import static com.example.casper.Experiment2024.Test.ImageUtils.areImagesSame;
import static com.example.casper.Experiment2024.Test.ImageUtils.getCoverBitmap;

import android.content.Context;


import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import androidx.test.platform.app.InstrumentationRegistry;

import com.example.casper.Experiment2024.R;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class DataBankTest {
    private Context appContext;
    private DataBank dataBankBackup;
    private final String tempFileName = "temp_Book.data";

    @Before
    public void setUp() {
        appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        dataBankBackup=new DataBank(appContext);
        List<Book> originalBooks = new ArrayList<>(dataBankBackup.readBooks());
        copyBooksToFile(originalBooks);
    }

    @After
    public void tearDown() {
        List<Book> tempBooks = readBooksFromFile();
        dataBankBackup.saveBooks(tempBooks);
        appContext.deleteFile(tempFileName);
    }

    @Test
    public void saveandreadBooks() {
        List<Book> Books=new ArrayList<>();
        Books.add(new Book("创新工程实践", R.drawable.book_no_name));
        Books.add(new Book("软件项目管理案例教程（第3版）", R.drawable.book_2));
        Books.add(new Book("信息安全数学基础（第2版）", R.drawable.book_1));
        DataBank dataBank=new DataBank(appContext);
        dataBank.saveBooks(Books);
        List<Book> readBooks =dataBank.readBooks();
        Assert.assertNotSame(Books,readBooks);
        for (int i = 0; i < Books.size(); i++) {
            Assert.assertNotSame(Books.get(i),readBooks.get(i));
            Assert.assertEquals(Books.get(i).getTitle(),readBooks.get(i).getTitle());
            Assert.assertTrue(areImagesSame(getCoverBitmap(Books.get(i).getCoverResourceId(),appContext), getCoverBitmap(readBooks.get(i).getCoverResourceId(),appContext)));
        }

    }

    private void copyBooksToFile(List<Book> books) {
        FileOutputStream fileout;
        try {
            fileout = appContext.openFileOutput(tempFileName, Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fileout);
            out.writeObject(books);
            out.close();
            fileout.close();
        } catch (IOException ignored) {
        }

    }

    private List<Book> readBooksFromFile() {
        List<Book> book=new ArrayList<>();
        try {
            FileInputStream fileIn= appContext.openFileInput(tempFileName);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            book = (List<Book>) in.readObject();
            in.close();
            fileIn.close();
        } catch (IOException | ClassNotFoundException ignored) {
        }
        return book;
    }
}